package com.gitee.zeric.codec.controller;


import com.gitee.zeric.codec.constans.CommonConstants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel("返回信息主体")
public class R<T> implements Serializable {
    private static final String FAIL_MSG = "Failed";
    private static final String SUCCESS_MSG = "Succeed";
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("返回标记：成功标记=0，失败标记=1")
    private int code;
    @ApiModelProperty("消息提示")
    private String msg;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("时间戳")
    private long timestamp = System.currentTimeMillis();
    @ApiModelProperty("流水号")
    private String nonce;
    @ApiModelProperty("签名信息")
    private String sign;

    public static <T> R<T> ok() {
        return restResult((T) null, CommonConstants.SUCCESS, "Succeed");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, "Succeed");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult((T) null, CommonConstants.FAIL, "Failed");
    }

    public static <T> R<T> failed(String msg) {
        return restResult((T) null, CommonConstants.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, "Failed");
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R();
        apiResult.setCode(code);
        apiResult.setData(data);
         //String message = Resources.getMessage(msg, new Object[0]);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> RBuilder<T> builder() {
        return new RBuilder();
    }

    public String toString() {
        return "R(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ", timestamp=" + this.timestamp + ", nonce=" + this.nonce + ", sign=" + this.sign + ")";
    }

    public R() {
    }

    public R(int code, String msg, T data, long timestamp, String nonce, String sign) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.timestamp = timestamp;
        this.nonce = nonce;
        this.sign = sign;
    }

    public int getCode() {
        return this.code;
    }

    public R<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public static class RBuilder<T> {
        private int code;
        private String msg;
        private T data;
        private long timestamp;
        private String nonce;
        private String sign;

        RBuilder() {
        }

        public RBuilder<T> code(int code) {
            this.code = code;
            return this;
        }

        public RBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public RBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public RBuilder<T> timestamp(long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public RBuilder<T> nonce(String nonce) {
            this.nonce = nonce;
            return this;
        }

        public RBuilder<T> sign(String sign) {
            this.sign = sign;
            return this;
        }

        public R<T> build() {
            return new R(this.code, this.msg, this.data, this.timestamp, this.nonce, this.sign);
        }

        public String toString() {
            return "R.RBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ", timestamp=" + this.timestamp + ", nonce=" + this.nonce + ", sign=" + this.sign + ")";
        }
    }

}
